package com.littleu.yojoincn.controller.tt;

import com.littleu.yojoincn.common.util.JwtToken;
import com.littleu.yojoincn.dto.tt.LoginDTO;
import com.littleu.yojoincn.dto.tt.TokenDTO;
import com.littleu.yojoincn.service.TtAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/tt/token")
@Validated
public class TokenController {
    @Autowired
    private TtAuthService ttAuthService;

    @PostMapping("")
    public Map<String, String> getToken(@RequestBody @Validated LoginDTO dto) {
        Map<String, String> map = new HashMap<>();
        String token = this.ttAuthService.code2Session(dto.getCode());
        map.put("token", token);
        return map;
    }

    @PostMapping("/verify")
    public Map<String, Boolean> verifyToken(@RequestBody @Validated TokenDTO dto){
        Map<String, Boolean> map = new HashMap<>();
        Boolean valid = JwtToken.verifyToken(dto.getToken());
        map.put("is_valid", valid);
        return map;
    }
}
